<template>
  <div class="login-container">
    <el-card class="login-card">
      <div class="title-container">
        <h3 class="title">宠物商店管理系统</h3>
      </div>
      
      <el-form ref="loginFormRef" :model="loginForm" :rules="loginRules" class="login-form">
        <el-form-item prop="userName">
          <el-input
            v-model="loginForm.userName"
            placeholder="用户名"
            type="text"
            prefix-icon="User"
          />
        </el-form-item>
        
        <el-form-item prop="password">
          <el-input
            v-model="loginForm.password"
            placeholder="密码"
            type="password"
            prefix-icon="Lock"
            show-password
          />
        </el-form-item>
        
        <el-button :loading="loading" type="primary" class="login-button" @click="handleLogin">
          登录
        </el-button>
      </el-form>
    </el-card>
  </div>
</template>

<script setup>
import { ref, reactive, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import { login } from '@/api/user'

const router = useRouter()
const loading = ref(false)
const loginFormRef = ref(null)

const loginForm = reactive({
  userName: '',
  password: ''
})

const loginRules = {
  userName: [{ required: true, message: '请输入用户名', trigger: 'blur' }],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' }, 
    { min: 4, message: '密码长度不能小于4位', trigger: 'blur' }
  ]
}

// 页面加载时检查URL参数
onMounted(() => {
  // 检查URL中是否有表示过期的参数
  const urlParams = new URLSearchParams(window.location.search)
  if (urlParams.get('expired') === 'true') {
    ElMessage({
      message: '登录状态已过期，请重新登录',
      type: 'warning'
    })
    // 清除URL参数
    window.history.replaceState({}, document.title, window.location.pathname)
  }
  
  // 清除任何可能存在的token
  localStorage.removeItem('token')
  sessionStorage.removeItem('token')
})

const handleLogin = () => {
  loginFormRef.value.validate(async valid => {
    if (!valid) return
    
    try {
      loading.value = true
      const response = await login(loginForm)
      
      if (response.code === 200) {
        // 保存token到localStorage和sessionStorage
        localStorage.setItem('token', response.data)
        sessionStorage.setItem('token', response.data)
        
        // 导航到首页
        router.push('/')
        ElMessage({
          message: '登录成功',
          type: 'success'
        })
      } else {
        ElMessage({
          message: response.msg || '登录失败',
          type: 'error'
        })
      }
    } catch (error) {
      console.error('登录失败:', error)
      ElMessage({
        message: '登录失败，请稍后再试',
        type: 'error'
      })
    } finally {
      loading.value = false
    }
  })
}
</script>

<style scoped>
.login-container {
  height: 100%;
  width: 100%;
  background-color: #2d3a4b;
  display: flex;
  justify-content: center;
  align-items: center;
}

.login-card {
  width: 400px;
}

.title-container {
  text-align: center;
  margin-bottom: 30px;
}

.title {
  font-size: 26px;
  color: #303133;
  margin: 0;
}

.login-form {
  width: 100%;
}

.login-button {
  width: 100%;
  margin-top: 10px;
}
</style> 